package org.shopping.dao.admin.impl;

import org.shopping.dao.admin.AdminDao;
import org.shopping.domain.Admin;
import org.shopping.utils.JDBCUtils2;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class AdminDaoImpl implements AdminDao {

    // 创建模板类对象
    private JdbcTemplate jt = new JdbcTemplate(JDBCUtils2.getDataSource());

    @Override
    public Admin findByNameAndPassword(String nameForm, String pswForm) {
        try {
            // 查询成功
            // 编写sql语句
            String sql = "select * from tab_admin where username=? and password=?";
            // 执行查询方法
            Admin admin = jt.queryForObject(sql,
                    new BeanPropertyRowMapper<Admin>(Admin.class),
                    nameForm,
                    pswForm);
            return admin;
        } catch (DataAccessException e) {
            // 查询失败
            return null;
        }
    }

    @Override
    public int addAdmin(Admin adminForm) {
        // 编写sql
        String sql = "insert into tab_admin values(?,?,?)";
        // 调用更新方法
        int row = jt.update(sql,
                adminForm.getAid(),
                adminForm.getUsername(),
                adminForm.getPassword());
        return row;
    }

    @Override
    public Admin findAdminByCount(String count) {
        try {
            // 查询成功
            // 编写sql语句
            String sql = "select * from tab_admin where username=?";
            // 执行查询方法
            Admin admin = jt.queryForObject(sql,
                    new BeanPropertyRowMapper<Admin>(Admin.class),
                    count);
            return admin;
        } catch (DataAccessException e) {
            e.printStackTrace();
            // 查询失败
            return null;
        }
    }
}
